/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl3.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import pl3.GIC;

/**
 * Deals with the input file that contains the grammar and converts it to a {@link GIC GIC object}.
 *
 * @author Luis Alberto Pérez García <luixal@gmail.com>
 * @version 0.1
 * @see GIC
 */
public class Input {

    String filename;
    BufferedReader ibr;


    /**
     * Default class contructor for Input class.
     */
    public Input() {
        this.filename = "";
    }
    /**
     * Parametrized class constructor for Input class.
     *
     * @param filename Name of the file containing the grammar
     */
    public Input(String filename) {
        this.filename = filename;
    }

    /**
     * Reads the input file converting it to a {@link GIC GIC Object}.
     *
     * @return An {@link GIC GIC Object} contaning the grammar specified in the file.
     * @throws IOException
     */
    public GIC readFile() throws IOException {
        // we open the file:
        this.ibr = new BufferedReader(new FileReader(this.filename));
        this.ibr.mark(500);
        // declare variables AFTER the file is opened (this way no extra memory is occupied if file can't  be read):
        GIC gic = new GIC();
        String aux = this.ibr.readLine();
        // reading the grammar...
        while (aux != null) {
            StringTokenizer stk = new StringTokenizer(aux, "->");
            gic.putProduction(stk.nextToken().trim(), stk.nextToken().trim());
            aux = this.ibr.readLine();
        }
        // we close the file to release the resource and return the grammar:
        this.ibr.close();
        return gic;
    }

    /**
     * Same method that {@link Input#readFile() Input.readFile()}, but implmemented in a <b>static</b> way.
     *
     * @param filename The name of a file containing a valid grammar
     * @return An {@link GIC GIC Object} contaning the grammar specified in the file.
     * @throws IOException
     */
    public static GIC readFile(String filename) throws IOException {
        BufferedReader ibr;
        // we open the file:
        ibr = new BufferedReader(new FileReader(filename));
        ibr.mark(500);
        // declare variables AFTER the file is opened (this way no extra memory is occupied if file can't  be read):
        GIC gic = new GIC();
        String aux = ibr.readLine();
        // setting initSymbol:
        if (aux != null) gic.setInitSymbol(aux.split("->")[0].trim());
        // reading the grammar...
        while (aux != null) {
            StringTokenizer stk = new StringTokenizer(aux, "->");
            gic.putProduction(stk.nextToken().trim(), stk.nextToken().trim());
            aux = ibr.readLine();
        }
        // we close the file to release the resource and return the grammar:
        ibr.close();
        return gic;
    }

    /**
     * An static method that reads a file containing and input to be evaluated by the grammar.
     *
     * @param filename The name of a file containing an input
     * @return An {@link ArrayList ArrayList of Strings} contaning the inputs read from the file
     * @throws IOException
     */
    public static ArrayList<String> readInputFile(String filename) throws IOException {
        BufferedReader ibr;
        // we open the file:
        ibr = new BufferedReader(new FileReader(filename));
        ibr.mark(500);
        // declare variables AFTER the file is opened (this way no extra memory is occupied if file can't  be read):
        ArrayList<String> inputs = new ArrayList<String>();
        String aux = ibr.readLine();
        // reading the inputs...
        while (aux != null) {
            inputs.add(aux);
            aux = ibr.readLine();
        }
        // we close the file to release the resource and return the input content:
        ibr.close();
        return inputs;
    }
}
